//
//  CountryMaster.m
//  DataCollector
//
//  Created by jiro on 12/06/11.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "CrudeOilProductCountryCode.h"

#import "../DbConstants.h"

@implementation CrudeOilProductCountryCode

+ (id) crudeOilProductCountryCodeWithValues: (NSArray*) values
{
	CrudeOilProductCountryCode* ret = [ [ CrudeOilProductCountryCode alloc ] init ];
	ret.code = [ values objectAtIndex: 0 ];
	ret.name = [ values objectAtIndex: 1 ];
	return ret;
}

+ (NSString*) tableName
{
	return kText_CrudeOilProductCountryTable;
}

+ (NSString*) queryCreateTable
{
	NSString* s = [ NSString stringWithFormat:
		@"create table %@( %@ %@, %@ %@, %@( %@ ) );",
			kText_CrudeOilProductCountryTable,
			kText_Code, kText_Text,
			kText_Name, kText_Text,
			kText_PrimaryKey, kText_Code
	];
	return s;
}

- (NSString*) sqlInsertStatement
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ values %@;",
		[ CrudeOilProductCountryCode tableName ], [ self queryInsertValue ] ];
	return s;
}

+ (NSString*) queryInsertInitialValues
{
	struct CodeName { int code; NSString* name; };
	CodeName codeNames[] = {
		{ CrudeOilProductCountryCodeType::TotalWorld, @"TotalWorld" },
		{ CrudeOilProductCountryCodeType::UnitedStates, @"UnitedStates" },
		{ CrudeOilProductCountryCodeType::OPECAverage, @"OPECAverage" },
		{ CrudeOilProductCountryCodeType::AbuDhabi, @"AbuDhabi" },
		{ CrudeOilProductCountryCodeType::Algeria, @"Algeria" },
		{ CrudeOilProductCountryCodeType::Angola, @"Angola" },
		{ CrudeOilProductCountryCodeType::Dubai, @"Dubai" },
		{ CrudeOilProductCountryCodeType::Ecuador, @"Ecuador" },
		{ CrudeOilProductCountryCodeType::Iran, @"Iran" },
		{ CrudeOilProductCountryCodeType::Kuwait, @"Kuwait" },
		{ CrudeOilProductCountryCodeType::Libya, @"Libya" },
		{ CrudeOilProductCountryCodeType::NeutralZone, @"NeutralZone" },
		{ CrudeOilProductCountryCodeType::Nigeria, @"Nigeria" },
		{ CrudeOilProductCountryCodeType::Qatar, @"Qatar" },
		{ CrudeOilProductCountryCodeType::SaudiArabia, @"SaudiArabia" },
		{ CrudeOilProductCountryCodeType::Venezuela, @"Venezeula" },
		{ CrudeOilProductCountryCodeType::Australia, @"Australia" },
		{ CrudeOilProductCountryCodeType::Brunei, @"Brunei" },
		{ CrudeOilProductCountryCodeType::Cameroon, @"Cameroon" },
		{ CrudeOilProductCountryCodeType::Canada, @"Canada" },
		{ CrudeOilProductCountryCodeType::China, @"China" },
		{ CrudeOilProductCountryCodeType::Colombia, @"Colombia" },
		{ CrudeOilProductCountryCodeType::Egypt, @"Egypt" },
		{ CrudeOilProductCountryCodeType::Gabon, @"Gabon" },
		{ CrudeOilProductCountryCodeType::Indonesia, @"Indonesia" },
		{ CrudeOilProductCountryCodeType::Malaysia, @"Malaysia" },
		{ CrudeOilProductCountryCodeType::Mexico, @"Mexico" },
		{ CrudeOilProductCountryCodeType::Norway, @"Norway" },
		{ CrudeOilProductCountryCodeType::Oman, @"Oman" },
		{ CrudeOilProductCountryCodeType::Russia, @"Russia" },
		{ CrudeOilProductCountryCodeType::UnitedKingdom, @"UnitedKingdom" },	
		{ CrudeOilProductCountryCodeType::NonOPECAverage, @"NonOPECAverage" },
	};

	NSString* s = [ NSString string ];
	for ( int i = 0, count = sizeof( codeNames ) / sizeof( CodeName ); i < count; i++ ) 
	{
		CrudeOilProductCountryCode* code = [ [ CrudeOilProductCountryCode alloc ] init ];
		const CodeName& codeName = codeNames[ i ];
		code.code = [ NSString stringWithFormat: @"%d", codeName.code ];
		code.name = codeName.name;
		s = [ s stringByAppendingString:[ code queryInsertValue ] ];
		if ( i != count - 1 )
		{
			s = [ s stringByAppendingString: @", " ];
		}
	}
	return s;
}

+ (NSString*) querySelectAll
{
	NSString* s = [ NSString stringWithFormat: @"select * from %@;", [ CrudeOilProductCountryCode tableName ] ];
	return s;
}

- (NSString*) queryInsert
{
	NSString* s = [ NSString stringWithFormat: @"insert into * values %@;", 
		[ CrudeOilProductCountryCode tableName ], [ self queryInsertValue ] ];
	return s;
}

- (NSString*) queryInsertValue
{
	NSString* s = [ NSString stringWithFormat: @"( '%@', '%@' )", code, name ];
	return s;
}

@end
